﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WordTemplate1.Formulaire
{
    public partial class AddLink : Form
    {
        public WorkItem Work { get; set; }

        public AddLink()
        {
            InitializeComponent();
        }

        private void AddLink_Load(object sender, EventArgs e)
        {
            // Met à jour la liste des requêtes
            cbxQueries.DataSource = Globals.ThisDocument.taskPanel.ListeQuery;
            cbxQueries.DisplayMember = "Name";


            var liste = Globals.ThisDocument.taskPanel.WorkItems;

            foreach (var item in liste)
            {
                ListViewItem lvi;
                lvi = new ListViewItem(item.Id.ToString());
                lvi.SubItems.Add(item.Type.Name);
                lvi.SubItems.Add(item.Title);
                lvi.SubItems.Add(item.State);
                lvi.SubItems.Add(item.Description);
                lvi.Tag = item;
                this.listView1.Items.Add(lvi);
            }

        }

        private void btnValider_Click(object sender, EventArgs e)
        {
            Work.Open();

            foreach (ListViewItem lvi in this.listView1.SelectedItems)
            {
                WorkItem workEnfant = lvi.Tag as WorkItem;

                if (Work.Id != workEnfant.Id)
                    Work.Links.Add(new RelatedLink(workEnfant.Id));
            }

            if (Work.IsValid())
                Work.Save();

            Globals.ThisDocument.taskPanel.Actualise();
            this.Close();
        }

        private void btnAnnuler_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
